package com.wx.tnsbasedata.controller;

import com.wx.common.api.CommonResult;
import com.wx.tnsbasedata.model.CityModel;
import com.wx.tnsbasedata.model.vo.CityOrderVo;
import com.wx.tnsbasedata.model.vo.CityVo;
import com.wx.tnsbasedata.service.CityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/24 17:12
 */
//城市数据
@CrossOrigin
@RestController
@RequestMapping("/city")
@Api(tags = "城市数据")
public class CityController {

    @Autowired
    private CityService cityService;

    //前端需求接口

    //获取(市级)城市顺序排放
    @GetMapping("/allOrder")
    @ApiOperation(value = "获取(市级)城市顺序排放", notes = "获取(市级)城市顺序排放")
    public CommonResult<List<CityOrderVo>> findAllOrder() {
        List<CityOrderVo> list = null;
        try {
            list = cityService.findAllOrder();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取城市数据失败！");
        }
        return CommonResult.success(list);
    }

    //获取全部城市数据
    @GetMapping("/all")
    @ApiOperation(value = "获取全部城市数据", notes = "获取全部城市数据")
    public CommonResult<CityVo> findAll() {
        CityVo city = null;
        try {
            city = cityService.findAll();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取城市数据失败！");
        }
        return CommonResult.success(city);
    }

    //测试Service接口
    @PostMapping("/insert")
    @ApiOperation(value = "添加城市", notes = "添加城市")
    public CommonResult insert(@RequestBody CityModel cityModel) {
        int result = 0;
        try{
            result = cityService.insert(cityModel);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("添加失败！");
        }
        return CommonResult.success("添加"+result+"条数据成功！");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改城市数据", notes = "修改城市数据")
    public CommonResult update(@RequestBody CityModel cityModel) {
        int result = 0;
        try{
            result = cityService.update(cityModel);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("修改失败！");
        }
        return CommonResult.success("修改"+result+"条数据成功！");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除城市数据", notes = "删除城市数据")
    public CommonResult delete(@RequestParam Integer id) {
        int result = 0;
        try{
            result = cityService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.success("删除失败！");
        }
        return CommonResult.success("删除"+result+"条数据成功！");
    }
}
